package Ui;

import DBase.Board;
import DBase.Coordinate;
import java.util.Scanner;

    
/**
 * 
 * @author Elya & Dana
 */
public  abstract class GameUi {
    
   
    Scanner s = new Scanner(System.in); 
    
    /**
     * @param shipSizesArray
     * Get ships names array
     * @return ships names array
     */
    public abstract String [] getShipsData(int [] shipSizesArray);
    
    /**
     * Get the players number
     * @return
     */
    public abstract int getPlayersNumber();
    
    /**
     * Get players type array
     * @param numPlayers
     * @return integer player type array
     */
    public abstract int [] getPlayersType(int numPlayers);
    
    /**
     * Gets player names array
     * @param numPlayers
     * @return string array
     */
    
    
    public void printComputerchoice(int boardNum,String name){
        
        System.out.println(name+" chooce boars number "+boardNum);
        
    }
    public abstract String [] getPlayersNames(int numPlayers);
    
    /**
     * Places a ship on the board
     * @param x row
     * @param y column
     * @param direction Horizontal or Vertical
     * @param shipSize
     * @param PlayerName
     * @param Name ship name
     */
    public abstract void PlaceShipsOnBoard(int[] x,int[] y,int[] direction,int shipSize,String PlayerName,String Name);
    
    /**
     * Converts a given string to a coordinate object
     * @param strPosition the coordinate string
     * @return Coordinate-if invalid returns (-1,-1) coordinate otherwise the correct coordinate
     */
    public Coordinate convertStrCoordinateToInt(String strPosition){

        int column=-1;
        int row=-1;
        
        if (strPosition.length()==2 || strPosition.length()==3){
        
            char c=strPosition.charAt(0);//the column

            //column
            if(c>='A' && c<='J')
                column=c-'A';
            else if(c>='a' && c<='j')
                column=c-'a';


            char digit1=strPosition.charAt(1);//first digit of row

            if (strPosition.length()==2){

                if (digit1>='1' && digit1<='9')//only one digit 1-9
                    row=digit1-'1';
            }

            else if(strPosition.length()==3){//double digit
                char digit2=strPosition.charAt(2);
                //theres a second digit.check if it's a 10
                if(digit1=='1' && digit2=='0'){
                    row=9;
                }

             }
        }
        Coordinate intCoordinate=new Coordinate(row,column);
        
        return intCoordinate;
    }

    
    
    
    /**
     * Asks user for a coordinate input and checks if it's correct
     * @return Coordinate
     */
    public Coordinate getCoordinateInput(){
        
        String input;

        System.out.println("Enter the Coordinate,it must be a letter followed by a number.For Example: A5");

        int row=-1;
        int column=-1;
        Coordinate coordinate;
        
        while ( (row==-1) || (column==-1) ){
            
            input=s.nextLine();
            
            //input = s.next();
            coordinate=convertStrCoordinateToInt(input);
            row=coordinate.getRow();
            column=coordinate.getColumn();
            
            //check if correct
            if ( (row==-1) || (column==-1) ){
                System.out.println("\nMust be a letter followed by a number e.g.  A5");
                //input="";
            }

            
        }
        
        return new Coordinate(row,column);
    }
    
        
    /**
     * Gets the board number of player to attack
     * @param numPlayers
     * @return boardNum
     */
    public  int getBoardNum(int numPlayers){
        
       int boardNum=0;
       
       String input;
       
      while(boardNum <= 0 || boardNum > numPlayers){
            System.out.println("\nPlease enter the number of the player's board you want to attack:");
            input=s.nextLine();
            
            if (input.length()==1){
                boardNum =(int)(input.charAt(0)-'0');
                if(boardNum <= 0 || boardNum > numPlayers)
                    System.out.println("Oops! wrong input! Please try again.");
            }
            else
                System.out.println("Oops! wrong input! Please try again.");
       }
       
       
       return boardNum;
    }
    
    
    /**
     * Gets the choice of the players move-mark,un mark,bomb
     * @return choice
     */
    public int getChoice(){
        
        int choice=0;
        String input;
        boolean isValid=false;

        System.out.println("\nTo mark a square press 1.");
        System.out.println("To unmark a square press 2.");
        System.out.println("To bomb a square press 3.");
        input=s.nextLine();

        isValid=( "1".equals(input) || "2".equals(input) || "3".equals(input));
        while (isValid==false){
            System.out.println("Oops!Wrong input.Please Try Again:");
            System.out.println("To mark a square press 1");
            System.out.println("To unmark a square press 2");
            System.out.println("To bomb a square press 3");
            
            input=s.nextLine(); 
            isValid=( "1".equals(input) || "2".equals(input) || "3".equals(input));
        }
        choice=input.charAt(0)-'0';//convert to int
            
        return choice;
    }

 
    /**
     * Get marked cell
     * @return Coordinate
     */
    public Coordinate getMarkedSquare(){

        System.out.println("\nPlease enter the coordinate you want to mark:");
        return getCoordinateInput();
    }
    /**
     * Gets bombed cell
     * @return Coordinate
     */
    public Coordinate getBombedSquare(){
        
        System.out.println("\nPlease enter the coordinate you want to bomb:");
        return getCoordinateInput();
    }  
    /**
     * Gets unMarked cell
     * @return Coordinate
     */
    public Coordinate getUnMarkedSquare(){
        
        System.out.println("\nPlease enter the coordinate you want to unmark:");
        return getCoordinateInput();
    }
    
    
    /**
     * Prints how you want to continue that game-start new game or quit
     * @return integer with your choice
     */
    public int howToContinue(){
        
        int choice=0;
        String input;
        boolean isValid=false;

        System.out.println("\nTo start a new Game with the same Data press 1.");
        System.out.println("To start a new game with new Data press 2.");
        System.out.println("To Quit playing press 3;");
        input=s.nextLine();
        
        isValid=( "1".equals(input) || "2".equals(input) || "3".equals(input));

        while (isValid==false){
            System.out.println("\nOops!Wrong input.Please Try Again:");
            System.out.println("To start a new Game with the same Data press 1.");
            System.out.println("To start a new game with new Data press 2.");
            System.out.println("To Quit playing press 3;");
            
            input=s.nextLine();
            isValid=( "1".equals(input) || "2".equals(input) || "3".equals(input));
        }
        choice=input.charAt(0)-'0';//convert to int
        
        return choice;
    }
    
     /**
     *Prints that you can't choose an inactive board
     */
    public void chooseNotActiveBoard(){
          
        System.out.println("\nYou can't choose a board that's not active! Try again:");
    }
    
     /**
     * Asks if you want to quit the game
     * @return true or false
     */
    public boolean quitGame(){
        
        boolean ans;
        String input;
        System.out.println("\nIf you wish to quit press 1 otherwise press 2.");
        input = s.nextLine();
        while( !("1".equals(input)) && !("2".equals(input))){
            System.out.println("\nWrong input.If you wish to quit press 1 otherwise press 2.");
            input = s.nextLine();
        }
        if("1".equals(input))
            ans = true;
        else ans = false;
        
        return ans;
    }
    
    /**
     *Prints that you can't choose your own board
     */
    public void chooseWrongBoard(){
         System.out.print("\nYou can't choose your own board! Try again:");
    }
    /**
     * Prints the rivals board data: the number of active and inactive ships
     * @param active how many ships are active
     * @param notActive how many ships aren't active
     */
    public void printRivalBoardData(int active ,int notActive){

        System.out.println("\nNumber of ships active :" + active);
        System.out.println("Number of ships  not active :" + notActive);

    }
    
    /**
     * Announces the winner of the game
     * @param name player name
     */
    public void announceWinner(String name){
       
        System.out.println("\n\nGAME OVER!!! The Winner is :" + name);
    }
    
    /**
     * Announces the players turn
     * @param name the player name
     */
    public void AnnounchPlayerTurn(String name){
        
        System.out.println("\n\n\nIt's " + name + "'s turn in game!!!");
        System.out.println("His/Her rivals boards are:");
    }
    
    
    /**
     * Announces computer players move in the game(which ship he attacked)
     * @param board the board number
     * @param square the coordinate
     * @param name computer name
     */
    public void AnnounchPlayerComputerChoice(int board, Coordinate square ,String name){
        board=board+1;
        System.out.println(name + " choose to attack board # " +board );
        char ch= (char)(square.getColumn()+(int)'A');
        System.out.println("square [ "+ ch+ " , "+ square.getRow()+" ]");
        
    }
    
    /**
     * Print player board
     * @param board the actual board
     * @param boardNumber
     * @param playerName
     */
    public void printBoard(Board board,int boardNumber,String playerName){   
       
        int num=1;
        System.out.println("\n");
        System.out.println(playerName + "'s" + " board. Board #" + boardNumber + ": \n");

        System.out.println("  A  B  C  D  E  F  G  H  I  J  ");
        System.out.println("===============================");
        for (int i=0; i< 10;i++){
            System.out.print("| ");
            for (int j = 0; j< 10; j++){
                System.out.print((board.GetGameBoard())[i][j].getMark());
                 System.out.print("| ");
            }
            System.out.println(num);
            System.out.println("===============================");
            num++;
        }
    }
    
 
     /**
     * Check if a name is valid (not empty and not a duplicate)
     * @param name
     * @param index the upper bound of the String array
     * @param arr
     * @return true if valid, false otherwise
     */
    public boolean checkIfNameValid(String name,int index,String[]arr){
    
        boolean ans=true;
        
        if(name.isEmpty()){
            ans=false;
        }
        for(int j=0; j<index;j++){
            if(name.compareToIgnoreCase(arr[j])==0)
                ans=false;
        }
        return ans;
    }
}
